nsbase.ajax = new function () {
    var _self = this;

    function main() {
        _self.request = request;
        _self.setDefualtAjaxSetup = setDefualtAjaxSetup;
    }

    function request(url, data, successHandler, errorHandler, async) {
        /// <signature>
        ///   <summary>Perform an asynchronous HTTP (Ajax) request.</summary>
        ///   <param name="url" type="String">A string containing the URL to which the request is sent.</param>
        ///   <param name="data" type="Object">The data as parameter that is sent to the server with the request.</param>
        ///   <param name="successHandler(data, textStatus, jqXHR)" type="Function">A callback function that is executed if the request succeeds.</param>
        ///   <param name="errorHandler(data, textStatus, jqXHR)" type="Function">A callback function that is executed if the request failed.</param>
        ///   <param name="async" type="Boolean">If the request is sent asynchronous.</param>
        /// </signature>
        /// <signature>
        ///   <summary>Perform an asynchronous HTTP (Ajax) request.</summary>
        ///   <param name="url" type="String">A string containing the URL to which the request is sent.</param>
        ///   <param name="data" type="Object">The data as parameter that is sent to the server with the request.</param>
        ///   <param name="successHandler(data, textStatus, jqXHR)" type="Function">A callback function that is executed if the request succeeds.</param>
        ///   <param name="errorHandler(data, textStatus, jqXHR)" type="Function">A callback function that is executed if the request failed.</param>
        /// </signature>
        /// <signature>
        ///   <summary>Perform an asynchronous HTTP (Ajax) request.</summary>
        ///   <param name="url" type="String">A string containing the URL to which the request is sent.</param>
        ///   <param name="data" type="Object">The data as parameter that is sent to the server with the request.</param>
        ///   <param name="successHandler(data, textStatus, jqXHR)" type="Function">A callback function that is executed if the request succeeds.</param>
        /// </signature>
        //setTimeout(function () {
        $.ajax({
            async: (async != null) ? !!async : true,
            type: "post",
            contentType:"application/json",
            url: url ,
            data: JSON.stringify(data),
            dataType: "json",
            success: successHandler,
            error: errorHandler
        });
    // }, 0);
    }

    function setDefualtAjaxSetup(opt) {
        $.ajaxSetup({
            dataFilter: function (data, type) {
                if (typeof (data) === 'string') {
                    if (type === "json") {
                        var jsonData = $.parseJSON(data);
                        // 兼容1.5版本抛出异常的方式
                        if (jsonData && jsonData.info!=null && jsonData.status!=null && jsonData.url!=null) {
                            nsbase.utility.errorDialog(jsonData.info);
                        }
                        
                        // 异常处理
                        if(jsonData && jsonData.type && jsonData.message){
                            var message = jsonData.message;
                            if (jsonData.type == nsbase.consts.exceptionTypes.CookieException) {
                                return;
                            } 
                            if (jsonData.type == nsbase.consts.exceptionTypes.InputValidateException) {
                                nsbase.utility.errorDialog(jsonData.message);
                                return;
                            } 
                            if (jsonData.type == nsbase.consts.exceptionTypes.PrivilegeException) {
                                nsbase.utility.errorDialog(jsonData.message);
                                return;
                            }
                            if (jsonData.type == nsbase.consts.exceptionTypes.OperationException){
                                nsbase.utility.errorDialog(jsonData.message);
                                return;
                            }
                            // 系统级别的异常，不给用户提示信息，但可以通过查看日志找到异常
                            if(jsonData.type == nsbase.consts.exceptionTypes.SystemException){
                                return;
                            }
                            if (typeof this.error == "function") {
                                this.error(this, "error", jsonData);
                            }
                            throw message;
                        }
                    }
                }
                return data;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var text = XMLHttpRequest.responseText;
            //alert(text||conf.defaultMsg);
            }
        });
    };

    main();
}();

nsbase.ajax.setDefualtAjaxSetup();